<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/5/10
 * Time: 9:18
 */

namespace SDXApi\Controller;


/**
 * Class SearchController
 * @package SDXApi\Controller
 */
class SearchController extends AppController
{
    /**
     * Created by Jerry
     */
    public function get_search_hot_keywords()
    {
        $hots = D('SearchHot')->get_search_hots();

        if (empty($hots)) {
            $hots = array();
        }
        else{
            $hots = self::shuffleAssoc($hots);
            $hots = array_merge($hots);
        }

        $data = array();
        $data['keywords'] = $hots[0];

        returnApiSuccess($data);
    }

    /**
     * Created by Jerry
     * @param $list
     * @return array
     */
    static protected function shuffleAssoc($list)
    {
        if (! is_array($list)) {
            return $list;
        }

        $keys = array_keys($list);
        shuffle($keys);

        $random = array();
        foreach ($keys as $key) {
            $random[$key] = $list[$key];
        }

        return $random;
    }
}